package org.tools.common;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyUtil {
	
	private String propUrl = "conf" + File.separator + "app.properties";
	private Properties prop;
	
	public PropertyUtil(){}
	public PropertyUtil(String propUrl){this.propUrl = propUrl;}
	
	public void setPropUrl(String propUrl) {
		this.propUrl = propUrl;
	}

	public void setProp(Properties prop) {
		this.prop = prop;
	}

	public void init(){
		init(propUrl);
	}
	
	public void init(String propUrl){
		if(null == prop) {
			prop = new Properties();
			InputStream is = null;
			try {
				ClassLoader cl = PropertyUtil.class.getClassLoader();
				if (cl != null) {
					is = cl.getResourceAsStream(propUrl);
				} else {
					is = ClassLoader.getSystemResourceAsStream(propUrl);
				}
				prop.load(is);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public String getProperty(String key){
		return getProperty(key, null);
	}
	
	public String getProperty(String key, String defValue){
		init();
		
		return prop.getProperty(key, defValue);
	}
}
